package com.sound.ampache;

/* Copyright (c) 2010 Brian Kirkman <kirkman.brian@gmail.com>
*
* +------------------------------------------------------------------------+
* | This program is free software; you can redistribute it and/or          |
* | modify it under the terms of the GNU General Public License            |
* | as published by the Free Software Foundation; either version 3         |
* | of the License, or (at your option) any later version.                 |
* |                                                                        |
* | This program is distributed in the hope that it will be useful,        |
* | but WITHOUT ANY WARRANTY; without even the implied warranty of         |
* | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the          |
* | GNU General Public License for more details.                           |
* |                                                                        |
* | You should have received a copy of the GNU General Public License      |
* | along with this program; if not, write to the Free Software            |
* | Foundation, Inc., 59 Temple Place - Suite 330,                         |
* | Boston, MA  02111-1307, USA.                                           |
* +------------------------------------------------------------------------+
*
* Special thanks to the following: 
* Kevin Purdy, author of Amdroid, for much of the code used in this project
* 
* Jorge Ramon, author of MiamiCoder blog, for many great BlackBerry examples
* published on his blog.
* 
* 
*/

import net.rim.device.api.ui.*;
import net.rim.device.api.ui.container.*;
import net.rim.device.api.ui.component.*;

class SearchScreen extends MainScreen {
    
    private EditField searchPhraseField;
    private ArtistsScreen artistsScreen;
    private AlbumsScreen albumsScreen;
    private PlaylistsScreen playlistsScreen;
    private TagsScreen tagsScreen;
    private SongsScreen songsScreen;
    
    private MenuItem searchArtistsMenu = new MenuItem("Artists",100,10) {
       public void run() {
           searchAmpache(0);
       } 
    };
    
    private MenuItem searchAlbumsMenu = new MenuItem("Albums",100,10) {
        public void run() {
            searchAmpache(1);
        } 
     };
     
     private MenuItem searchSongsMenu = new MenuItem("Songs",100,10) {
         public void run() {
             searchAmpache(2);
         } 
      };
      
      private MenuItem searchPlaylistsMenu = new MenuItem("Playlists",100,10) {
          public void run() {
              searchAmpache(3);
          } 
       };
       
       private MenuItem searchTagsMenu = new MenuItem("Tags",100,10) {
           public void run() {
               searchAmpache(4);
           } 
        };
        
        private MenuItem searchGlobalMenu = new MenuItem("Global (Returns Songs)",100,10) {
            public void run() {
                searchAmpache(5);
            } 
         };
    
    SearchScreen() { 
    	setTitle( new LabelField( "Search Ampache" , LabelField.ELLIPSIS | LabelField.USE_ALL_WIDTH ) );
        searchPhraseField = new EditField("Search phrase: ","",256,EditField.USE_ALL_WIDTH);
        this.add(searchPhraseField);
    }
    
    protected void makeMenu(Menu menu, int instance) {
        menu.add(searchArtistsMenu);
        menu.add(searchAlbumsMenu);
        menu.add(searchSongsMenu);
        menu.add(searchPlaylistsMenu);
        menu.add(searchTagsMenu);
        menu.add(searchGlobalMenu);
        menu.add(MenuItem.separator(searchGlobalMenu.getOrdinal() + 1));
        super.makeMenu(menu,instance);
    }
    
    private void searchAmpache(int searchType) {
    	
        String searchPhrase = searchPhraseField.getText().trim();
        if (null != searchPhrase && searchPhrase.length() > 0) {
        	switch (searchType) {
            case 0:
            	if (null == artistsScreen) {
            		artistsScreen = new ArtistsScreen();   
            	}
            	artistsScreen.setRowHeight();
            	artistsScreen.screenTitle = "Search Artists - " + searchPhrase;
            	UiApplication.getUiApplication().pushScreen(artistsScreen);
                artistsScreen.getArtists(searchPhrase);
            break;
            case 1:
            	if (null == albumsScreen) {
            		albumsScreen = new AlbumsScreen();   
            	}
            	albumsScreen.setRowHeight();
            	albumsScreen.screenTitle = "Search Albums - " + searchPhrase;
            	UiApplication.getUiApplication().pushScreen(albumsScreen);
                albumsScreen.getAlbums(searchPhrase);
            break;
            case 2:
            	if (null == songsScreen) {
            		songsScreen = new SongsScreen();
            	}
            	songsScreen.setRowHeight();
            	songsScreen.screenTitle = "Search Songs - " + searchPhrase;
            	UiApplication.getUiApplication().pushScreen(songsScreen);
                songsScreen.getSongs(searchPhrase);
            break;
            case 3:
            	if (null == playlistsScreen) {
            		playlistsScreen = new PlaylistsScreen();
            	}
            	playlistsScreen.setRowHeight();
            	playlistsScreen.screenTitle = "Search Playlists - " + searchPhrase;
            	UiApplication.getUiApplication().pushScreen(playlistsScreen);
                playlistsScreen.getPlaylists(searchPhrase);
            break;
            case 4:
            	if (null == tagsScreen) {
            		tagsScreen = new TagsScreen();
            	}
                tagsScreen.screenTitle = "Search Tags - " + searchPhrase;
                UiApplication.getUiApplication().pushScreen(tagsScreen);
                tagsScreen.getTags(searchPhrase);
            break;
            case 5:
            	if (null == songsScreen) {
            		songsScreen = new SongsScreen();
            	}
            	songsScreen.setRowHeight();
                songsScreen.screenTitle = "Global Search (Songs) - " + searchPhrase;
                UiApplication.getUiApplication().pushScreen(songsScreen);
                songsScreen.getSongs("search_songs", searchPhrase);
            break;
        }	
        	
        } else {
            Dialog.alert("You must enter a search phrase.");
        }
    }
    
    protected boolean onSavePrompt() {
        return true;    // Ignore the onSave prompt.
    }
} 

